package com.gp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gp.dao.BaseAreaDao;
import com.gp.dao.TbShopDao;
import com.gp.entity.BaseArea;
import com.gp.entity.TbShop;
import com.gp.service.CarShopService;
import com.gp.util.AddressUtis;
import com.gp.util.CommonResult;
import com.gp.util.ZhuanUtils;
import com.gp.vo.CarShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

@Service
public class CarShopServiceImpl implements CarShopService {

    @Autowired(required = false)
    private TbShopDao tbShopDao;
    @Autowired(required = false)
    private BaseAreaDao baseAreaDao;


    @Override
    public CommonResult openshoppage(Integer page, Integer limit, CarShopVo carShopVo) {
        QueryWrapper<TbShop> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(carShopVo.getName())) {
            wrapper.like("name", carShopVo.getName());
        }
        if (StringUtils.hasText(carShopVo.getAddress())) {
            wrapper.like("address", carShopVo.getAddress());
        }
        Page<TbShop> pg = new Page<>(page, limit);
        Page<TbShop> tbShopPage = tbShopDao.selectPage(pg, wrapper);
        return new CommonResult(200, "查询成功", tbShopPage);
    }

    @Override
    public CommonResult delCarById(Integer id) {
        int i = tbShopDao.deleteById(id);
        if (i > 0) {
            return new CommonResult(200, "删除成功", null);
        } else {
            return new CommonResult(500, "删除失败", null);
        }
    }

    @Override
    public CommonResult findFirstAdress(Integer id) {
        Map<String,Object> map=new HashMap<>();
        if (id==1){
            QueryWrapper<BaseArea> wrapper=new QueryWrapper<>();
            wrapper.eq("parentid",0);
            List<BaseArea> baseAreas = baseAreaDao.selectList(wrapper);
            List<AddressUtis> sheng = ZhuanUtils.zhuanghaun(baseAreas);

            map.put("sheng",sheng);
            return new CommonResult(200, "查询成功",map );
        }else {
            QueryWrapper<BaseArea> wrapper2=new QueryWrapper<>();
            wrapper2.eq("parentid",id);
            List<BaseArea> areas = baseAreaDao.selectList(wrapper2);
            if (areas==null){
                map.put("other2",null);
                return new CommonResult(200, "查询成功",map);
            }else {
            List<AddressUtis> list = ZhuanUtils.zhuanghaun(areas);
            map.put("other",list);
            return new CommonResult(200, "查询成功",map);
            }
        }

    }

    @Override
    public CommonResult insert(TbShop tbShop) {

        BaseArea pro = baseAreaDao.selectById(tbShop.getProvinceId());
        BaseArea city = baseAreaDao.selectById(tbShop.getCityId());
        if (tbShop.getAreaId()==-1){
            tbShop.setAddress(pro.getName()+city.getName());
        }else {
            BaseArea area = baseAreaDao.selectById(tbShop.getAreaId());
            tbShop.setAddress(city.getName()+area.getName()+ tbShop.getName());
        }

        tbShop.setAddtime(new Date());
        int insert = tbShopDao.insert(tbShop);
        return new CommonResult(200, "插入成功", null);
    }

    @Override
    public CommonResult update(TbShop tbShop) {
        if (tbShop.getAreaId()==-1){
            tbShop.setAreaId(null);
        }
        int i = tbShopDao.updateById(tbShop);
        if (i> 0) {
            return new CommonResult(200, "修改成功", null);
        } else {
            return new CommonResult(500, "修改失败", null);
        }
    }


}
